package entitiesfactory;

import java.lang.reflect.*;
import java.util.HashMap;

import api.controller.IAutoservice;
import api.entitites.IDataBase;
import api.entitites.IGarage;
import api.entitites.IMaster;
import api.entitites.IOrder;
import api.exportimportentities.IExportImportEntities;
import api.model.IDBManager;

public class EntitiesFactory {
	private String NOT_FOUND = "This type not found";
	private String NOT_OBJECT = "This object not create";
	private HashMap<Class, String> classMap = new HashMap<Class, String>();
	private static EntitiesFactory entitiesFactory;

	private EntitiesFactory() {
		classMap.put(IAutoservice.class, "controllers.Autoservice");
		classMap.put(IExportImportEntities.class,
				"exportimporterentities.ExportImportEntities");
		classMap.put(IDBManager.class, "model.workersWithData.DBManager");
		classMap.put(IGarage.class, "entities.Garage");
		classMap.put(IMaster.class, "entities.Master");
		classMap.put(IOrder.class, "entities.Order");
		classMap.put(IDataBase.class, "entities.DataBase");

	}

	public static EntitiesFactory getInstance() {
		if (entitiesFactory == null) {
			entitiesFactory = new EntitiesFactory();
		}
		return entitiesFactory;
	}

	public <T> Object getObject(Type cInterface) {
		try {
			String className = classMap.get(cInterface);
			Class currentClass = Class.forName(className);
			return currentClass.newInstance();
		} catch (ClassNotFoundException clex) {
			throw new RuntimeException(NOT_FOUND);
		} catch (Exception e) {
			throw new RuntimeException(NOT_OBJECT);
		}
	}

}
